/*
 * Copyright (C) 2013 Daniel Wasilew <daniel.wasilew@codeminer.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.codeminer.parts.application;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 *
 * @author Daniel Wasilew <daniel.wasilew@codeminer.org>
 */
public final class Application {

    private static final Logger log = LoggerFactory.getLogger(Application.class);
    private final Map<Class, ApplicationComponent> components = new LinkedHashMap<>();
    private final Map<Class, ApplicationComponent> startedComponents = new LinkedHashMap<>();

    public Application() {
    }

    public void addApplicationComponent(ApplicationComponent component) {
        this.components.put(component.getDescriptionClass(), component);
    }

    public boolean startup(ApplicationContext applicationContext) {
        for (Map.Entry<Class, ApplicationComponent> entry : components.entrySet()) {
            ApplicationComponent applicationComponent = entry.getValue();
            Class applicationDescriptor = entry.getKey();

            log.trace("starting [{}] as [{}]", applicationComponent.getClass(), applicationDescriptor);
            try {
                applicationComponent.startup(applicationContext, new ShutdownCallback() {

                    @Override
                    public void requestShutdown(String reason) {
                        log.info("shutdown requested. The reason is [" + reason + "]");
                        shutdown();
                    }
                });
            } catch (Exception ex) {
                log.error("exception occured while starting application:", ex);
                shutdown();
                return false;
            }

            log.trace("started [{}] as [{}]", applicationComponent.getClass(), applicationDescriptor);
            startedComponents.put(applicationDescriptor, applicationComponent);
        }

        return true;
    }

    public void shutdown() {
        List<Class> tmpStarted = new LinkedList<>(startedComponents.keySet());
        Collections.reverse(tmpStarted);
        for (Class applicationDescriptor : tmpStarted) {
            ApplicationComponent applicationComponent = startedComponents.get(applicationDescriptor);
            log.info("shutting down [{}]", applicationComponent.getClass());
            applicationComponent.shutdown();
            startedComponents.remove(applicationDescriptor);
        }
    }

    @SuppressWarnings("unchecked")
    public <T> T getApplicationPart(Class<T> classDescriptor) {
        return (T) this.startedComponents.get(classDescriptor);
    }
}
